<template>
    <div class="check-task">
        <eagle-input v-model="form.ChnName" label="姓名" required placeholder="请输入姓名" />

        <eagle-input label="头像上传">
            <template #input>
                <eagle-image-uploader :images.sync="form.Head" cnt="1"></eagle-image-uploader>
            </template>
        </eagle-input>

        <eagle-input label="手机号" v-model="form.Mobile" readonly />
        <!-- 输入密码 -->
        <eagle-input v-model="form.Password" type="password" label="密码" required placeholder="请输入密码" />
        <eagle-input v-model="form.ConfirmPassword" type="password" label="确认密码" required placeholder="请确认密码" />
        <eagle-input v-model="form.CompanyName" label="所绑定的公司" readonly />
        <eagle-radio-group required name="radio" label="选择您的身份" style="margin-top:10px" v-model="form.Identity">
            <eagle-radio name="1">我是公司成员</eagle-radio>
            <eagle-radio name="2">我是外部人员</eagle-radio>
        </eagle-radio-group>
        <template v-if="form.Identity=='2'">
            <eagle-input v-model="form.OrgTempCode" label="所属公司" placeholder="请输入所属公司" required>
            </eagle-input>
        </template>
        <template v-else>
            <eagle-input v-model="form.WorkCode" label="工号" placeholder="请输入工号" />

            <eagle-input class="input_text" :value="form.OrgName" label="所属部门" placeholder="请选择" clearable readonly @click="goOrgListItem">
                <template #button>
                    <van-icon name="cross" @click="clearOrgCode" />
                </template>
            </eagle-input>
            <eagle-input class="input_text" :value="form.PostName" label="所属岗位" placeholder="请选择" @click="goPostListItem" readonly>
                <template #button>
                    <van-icon name="cross" @click="clearPost" />
                </template>
            </eagle-input>

        </template>

        <div class="bottom-btns">
            <eagle-button type="info" class="submit-only" @click="onSubmit">注册</eagle-button>
            <!-- <eagle-button type="info" class="submit-only" @click="onSubmit2"
        >注册2</eagle-button
      > -->
        </div>

        <department-choose-dialog :show.sync="deptmentShow" @callback="_chooseDeptmentCb" :selectedValue="form.OrgCode" :isPage="false" url="/Public/GetChooseOrgToList" />

        <user-post-choose-dialog :show.sync="postShow" @callback="_choosePostCb" url="/Public/GetChoosePostToList" />
    </div>
</template>

<script>
import { Register } from "@/api/public";
import { GetRegisterCompany } from "@/api/public.js";

import DepartmentChooseDialog from "@/views/components/department/departmentChooseDialog.vue";
import UserPostChooseDialog from "@/views/components/user/userPostChooseDialog.vue";
export default {
    components: {
        DepartmentChooseDialog,
        UserPostChooseDialog,
    },
    data() {
        return {
            uploader: [],
            code: "",
            qrCode: "",
            form: {
                Identity: "1",
                ChnName: "",
                WorkCode: "",
                Head: "",
                OrgName: "",
                OrgCode: "",
                PostCode: "",
                PostName: "",
                CompanyCode: "",
                CompanyName: "",
                Mobile: this.$route.query.mobile,
            },
            deptmentShow: false,
            postShow: false,
        };
    },
    created() {
        this.form.VerifyCode = this.$route.query.verifyCode;
        this.form.Mobile = this.$route.query.mobile;
        this.getCompany();
    },
    methods: {
        getCompany() {
            GetRegisterCompany().then((res) => {
                if (res.data) {
                    this.form.CompanyCode = res.data.CompanyCode;
                    this.form.CompanyName = res.data.CompanyName;
                }
            });
        },
        // 部门
        goOrgListItem() {
            this.deptmentShow = true;
        },
        _chooseDeptmentCb(item) {
            this.form.OrgName = item.name;
            this.form.OrgCode = item.code;
            this.deptmentShow = false;
        },
        clearOrgCode() {
            this.form.OrgCode = "";
            this.form.OrgName = "";
            this.form.PostName = "";
            this.form.PostCode = "";
        },
        // 岗位
        goPostListItem() {
            this.postShow = true;
        },
        _choosePostCb(item) {
            this.$set(this.form, "PostName", item.name || "");
            this.$set(this.form, "PostCode", item.code || "");
            this.postShow = false;
        },
        clearPost() {
            this.form.PostName = "";
            this.form.PostCode = "";
        },
        // 表单提交
        onSubmit() {


            if (!this.form.ChnName) {
                this.$toast("请输入姓名");
                return;
            }
            if (!this.form.Password) {
                this.$toast("请输入密码");
                return;
            }
            if (!this.form.ConfirmPassword) {
                this.$toast("请确认密码");
                return;
            }
            if (this.form.Password !== this.form.ConfirmPassword) {
                this.$toast("两次密码输入不一致");
                return;
            }
            if (this.form.Identity == '2') {
                if (!this.form.OrgTempCode) {
                    this.$toast("请输入所属公司名称");
                    return;
                }
                this.form.OrgCode = "O:"+ this.form.OrgTempCode;
            }
            if(process.env.VUE_APP_PLANTFORM=="GGYJ"&&this.$store.state.openId ){
                console.log("OpenId ",this.$store.state.openId)
                this.form.OpenId= this.$store.state.openId
                this.form.IsTempUser=false
            }
            
            Register(this.form)
                .then((res) => {
                    this.$toast("注册成功");
                    setTimeout(() => {
                        this.$router.push(
                            "/index?num=" + Math.ceil(Math.random() * 100000)
                        );
                    }, 1500);
                })
                .catch((err) => {
                    console.log("失败");
                });
        },
        onSubmit2() {
            this.$axios.get("/Public/QRCodeVerify", {
                params: {
                    code: "eyJEYXRhIjoie1wiQ29kZVwiOlwiMDAwMDFcIixcIkNvbXBhbnlDb2RlXCI6bnVsbH0iLCJDb2RlVHlwZSI6IkNvbXBhbnkifQ%3d%3d",
                },
            });
        },
    },
};
</script>

<style lang="less" scoped>
.phone {
    position: relative;
    .code {
        position: absolute;
        right: 20px;
        top: 20px;
        font-size: 12px;
    }
}
.check-task {
    background-color: #e8f1f8;
    padding-bottom: 20px;
}
.check-task2 {
    background-color: #e8f1f8;
    position: absolute;
    padding-bottom: 20px;
}
.img-box {
    height: 50px;
    width: 50px;
    padding: 10px;
    position: absolute;
    left: 50%;
    top: 60px;
    background-color: #fff;
    border-radius: 50%;
    overflow: hidden;
}
.popup-title {
    line-height: 35px;
    padding: 10px;
    font-size: 15px;
    padding-left: 20px;
}
</style>
